﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace WFExtended.Twitter
{
    /// <summary>
    /// TweetWithMedia Request
    /// </summary>
    [DataContract]
    public class TweetWithMediaRequest : ARequestStream
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TweetWithMediaRequest"/> class.
        /// </summary>
        public TweetWithMediaRequest()
        {
            PossiblySensitive = false;
            DisplayCoordinates = true;
        }

        /// <summary>
        /// The text of your status update. 
        /// </summary>
        /// <value>
        /// The text of your status update. 
        /// </value>
        [DataMember]
        public string Text { get; set; }

        /// <summary>
        /// Gets or sets the media to upload
        /// </summary>
        /// <value>
        /// The media.
        /// </value>
        [DataMember]
        public Media Media { get; set; }

        /// <summary>
        /// Set to true for content which may not be suitable for every audience.
        /// </summary>
        /// <value>
        ///   <c>true</c> if [possibly sensitive]; otherwise, <c>false</c>.
        /// </value>
        [DataMember]
        public bool PossiblySensitive { get; set; }

        /// <summary>
        /// The ID of an existing status that the update is in reply to.
        /// </summary>
        /// <value>
        /// The ID of an existing status that the update is in reply to.
        /// </value>
        [DataMember]
        public string InReplyToStatusID { get; set; }

        /// <summary>
        /// The latitude of the location this tweet refers to.
        /// </summary>
        /// <value>
        /// The latitude.
        /// </value>
        [DataMember]
        public string Latitude { get; set; }

        /// <summary>
        /// The longitude of the location this tweet refers to.
        /// </summary>
        /// <value>
        /// The longitude.
        /// </value>
        [DataMember]
        public string Longitude { get; set; }

        /// <summary>
        /// A place in the world identified by a Twitter place ID. 
        /// </summary>
        /// <value>
        /// The place ID.
        /// </value>
        [DataMember]
        public string PlaceID { get; set; }

        /// <summary>
        /// Whether or not to put a pin on the exact coordinates a tweet has been sent from.
        /// </summary>
        /// <value>
        ///   <c>true</c> if [display coordinates]; otherwise, <c>false</c>.
        /// </value>
        [DataMember]
        public bool DisplayCoordinates { get; set; }

        /// <summary>
        /// Gets the stream request.
        /// </summary>
        /// <value>
        /// The stream request.
        /// </value>
        /// <exception cref="NotFoundException">
        /// You must supply Text Parameter;null
        /// or
        /// You must supply Media Parameter;null
        /// </exception>
        public override System.IO.Stream RequestStream
        {
            get
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>();

                if (String.IsNullOrEmpty(Text))
                    throw new NotFoundException("You must supply Text Parameter", null);
                else
                    parameters.Add("status", Text);

                if (Media == null)
                    throw new NotFoundException("You must supply Media Parameter", null);
                else
                    parameters.Add("media[]", Media);

                if(!String.IsNullOrEmpty(InReplyToStatusID))
                    parameters.Add("in_reply_to_status_id", InReplyToStatusID);

                if (!String.IsNullOrEmpty(PlaceID))
                    parameters.Add("place_id", PlaceID);

                if (!String.IsNullOrEmpty(Latitude)
                    && !String.IsNullOrEmpty(Longitude))
                {
                    parameters.Add("lat", Latitude);
                    parameters.Add("long", Longitude);
                }

                parameters.Add("possibly_sensitive", PossiblySensitive);
                parameters.Add("display_coordinates", DisplayCoordinates);

                return MultipartFormData.GetMultipartFormData(parameters);
            }
        }
    }
}
